package com.jishaokang.service.impl;

import com.jishaokang.base.Result;
import com.jishaokang.cache.ResultCache;
import com.jishaokang.dao.ChapterDAO;
import com.jishaokang.util.VideoUtil;
import com.jishaokang.model.dto.Chapter;
import com.jishaokang.service.ChapterService;
import com.jishaokang.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by NANA_Final on 2019/6/21.
 */
@Service
public class ChapterServiceImpl implements ChapterService {

    @Autowired
    private ChapterDAO chapterDAO;
    @Autowired
    private MessageUtil messageUtil;
    @Autowired
    private VideoUtil videoUtil;

    @Override
    public Result selectChapterByCourseId(Chapter chapter) {
        chapter.setPage((chapter.getPage()-1)*chapter.getNumber());
        int total = chapterDAO.selectTotalByCourseId(chapter);
        List<Chapter> chapters  = chapterDAO.selectByCourseId(chapter);
        return ResultCache.getListDataOk(total,"chapters",chapters);
    }

    @Override
    public Result selectChapterByChapterId(Chapter chapter) {
        Chapter chapters = chapterDAO.selectByChapterId(chapter);
        return ResultCache.getDataOk(chapters);
    }

    @Override
    public Result selectRealChapterByChapterId(Chapter chapter) {
        Chapter chapters = chapterDAO.selectRealByChapterId(chapter);
        return ResultCache.getDataOk(chapters);
    }

    @Override
    public Result selectRealChapterByCourseId(Chapter chapter) {
        chapter.setPage((chapter.getPage()-1)*chapter.getNumber());
        int total = chapterDAO.selectTotalByCourseId(chapter);
        List<Chapter> chapters  = chapterDAO.selectRealByCourseId(chapter);
        return ResultCache.getListDataOk(total,"chapters",chapters);
    }

    public static final String KEYSTRING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    public static final int len = KEYSTRING.length();

    @Override
    public Result updateChapterVideo(String token, int chapterId, MultipartFile chapterVideoFile, HttpServletRequest request) throws IOException {
        System.out.println(chapterId);
        Random random = new Random();
        StringBuilder sb = new StringBuilder(new Date().getTime()+"");
        for (int i = 0;i < 50;i++ )
            sb = sb.append(KEYSTRING.charAt(random.nextInt(len)));
        sb.append(".");
        sb.append(chapterVideoFile.getOriginalFilename().split("\\.")[1]);
        String path = request.getServletContext().getRealPath("/upload/video/");
        String fileName = sb.toString();
        File targetFile = new File(path, fileName);
        System.out.println(path+"   "+fileName);
        targetFile.createNewFile();
        try {
            chapterVideoFile.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Chapter chapter = new Chapter();
        chapter.setChapterId(chapterId);
        chapter.setChapterVideo("upload/video/"+fileName);
        chapterDAO.updateChapterVideo(chapter);
        return ResultCache.OK;
    }

    @Override
    public Result addChapter(Chapter chapter) {
        chapterDAO.insert(chapter);
        return ResultCache.OK;
    }

    @Override
    public Result updateChapter(Chapter chapter) {
        chapterDAO.update(chapter);
        return ResultCache.OK;
    }

    @Override
    public Result deleteChapter(Chapter chapter) {
        chapterDAO.delete(chapter);
        return ResultCache.OK;
    }

    @Override
    public Result selectChapterByState(Chapter chapter) {
        int total = chapterDAO.selectTotalByState(chapter);
        chapter.setPage((chapter.getPage()-1)*chapter.getNumber());
        List<Chapter> chapters  = chapterDAO.selectByState(chapter);
        return ResultCache.getListDataOk(total,"chapters",chapters);
    }

    @Override
    public Result updateChapterStateAllow(Chapter chapter,HttpServletRequest request) {
        chapter.setChapterState(0);
        chapterDAO.updateChapterState(chapter);
        Chapter chapterInDB = chapterDAO.selectRealByChapterId(chapter);

        String path = request.getServletContext().getRealPath("/upload/video/");
        System.out.println(path);
        System.out.println(chapter);
        videoUtil.getPartVideoForWindows(path,chapterInDB.getChapterVideo().substring(14));

        chapter.setChapterTrialVideo("upload/video/Trial_"+chapterInDB.getChapterVideo().substring(14));
        int userId = chapterDAO.selectUserIdByChapterId(chapterInDB.getCourseId());
        chapterDAO.updateChapterTrialVideo(chapter);
        messageUtil.sendSystemMessage("您的课程"+chapterInDB.getChapterName()+"已通过审核",userId);

        return ResultCache.OK;
    }

    @Override
    public Result updateChapterStateRefuse(Chapter chapter) {
        chapter.setChapterState(2);
        chapterDAO.updateChapterState(chapter);
        Chapter chapterInDB = chapterDAO.selectByChapterId(chapter);
        int userId = chapterDAO.selectUserIdByChapterId(chapterInDB.getCourseId());
        messageUtil.sendSystemMessage("您的课程"+chapterInDB.getChapterName()+"未通过审核，已被删除",userId);
        return ResultCache.OK;
    }

}